package com.lc.activiti.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.activiti.config.ConstantsPile;
import com.lc.activiti.config.Result;
import com.lc.activiti.sys.dao.SysRoleDao;
import com.lc.activiti.sys.entity.SysRole;
import com.lc.activiti.sys.service.SysRoleService;
import com.lc.activiti.utils.redis.RedisUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (SysRole)表服务实现类
 *
 * @author 骆超
 * @since 2021-06-28 13:54:57
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {

    @Override
    public Result saveRoleInfo(SysRole sysRole) {
        return Result.success(sysRole.insertOrUpdate());
    }

    @Override
    public Result getRoleList(SysRole sysRole) {
        Page<SysRole> sysRolePage = baseMapper.selectPage(sysRole.getPage(), Wrappers.lambdaQuery(SysRole.class).eq(SysRole::getDelFlag, 1).like(ObjectUtil.isNotEmpty(sysRole.getRoleName()),SysRole::getRoleName,sysRole.getRoleName()).orderByDesc(SysRole::getCreateDate));
        return Result.success(sysRolePage);
    }

    /**
     * 将角色-名称塞入redis中
     * @author 骆超
     * @date 2021/6/29
     */
    @Override
    public void setRoleMap() {
        List<SysRole> list = list();
        Map<Long, String> roleMap = list.stream().collect(Collectors.toMap(SysRole::getId, SysRole::getRoleName));
        //将roleMap存入redis中
        RedisUtil.set(ConstantsPile.ROLE_MAP,roleMap);
    }
}
